<template>
  <!--
    商品 功能列表

    @author wangyongkang ${authorEmail}
    @date 2022-03-17 23:48:40
  -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否有效" prop="isActive">-->
<!--        <el-input-->
<!--          v-model="queryParams.isActive"-->
<!--          placeholder="请输入是否有效"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="状态：">
        <el-select v-model="queryParams.isActive" placeholder="全部" clearable>
          <el-option
            v-for="item in ActiveOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ItemList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名" align="center" prop="name" />
      <el-table-column label="商品编号" align="center" prop="id" />
      <el-table-column label="库存" align="center" prop="stock" />
<!--      <el-table-column label="采购时间" align="left" prop="purchaseTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="状态" align="center" prop="isActive" >
        <template slot-scope="scope">
          <div v-if="scope.row.isActive === 0" style="color:#ef084e">下架</div>
          <div v-if="scope.row.isActive === 1" style="color:green">上架</div>
        </template>
      </el-table-column>

<!--      <el-table-column label="状态" prop="hasStatus">-->
<!--        <template slot-scope="scope">-->
<!--        <el-switch-->
<!--            v-model="scope.row.hasStatus"-->
<!--            :active-value="0"-->
<!--            :inactive-value="1"-->
<!--            @change="handleStatusChange(scope.row)"-->
<!--          ></el-switch>-->
<!--         </template>-->
<!--      </el-table-column>-->
      <el-table-column label="添加时间" align="center" prop="addTime">
        <template slot-scope="scope">
          <span>{{ parseDatetime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名" />
        </el-form-item>
<!--        <el-form-item label="商品编号" prop="code">-->
<!--          <el-input v-model="form.code" placeholder="请输入商品编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="上架状态" prop="isActive">
          <el-radio v-model="form.isActive" :label="1" >上架</el-radio>
          <el-radio v-model="form.isActive" :label="0">下架</el-radio>
        </el-form-item>
<!--        <el-form-item label="是否有效" prop="isActive">-->
<!--          <el-input v-model="form.isActive" placeholder="请输入是否有效" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItem,
  getItem,
  delItem,
  addItem,
  changeItemField,
  changeStatusItem,
  updateItem,
  exportItem } from "@/api/shop-manage/Item";
import  Condition  from "alinesno-ui/packages/Search/Condition"
export default {
  name: "Item",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 状态
      statusOptions:[] ,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      ItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        stock: null,
        purchaseTime: null,
        isActive: null,
      },
      // 查询参数配置对象
      queryParamsConfig: {
        name: Condition.like(),
        code: Condition.like(),
        stock: Condition.like(),
        purchaseTime: Condition.like(),
        isActive: Condition.like(),
      },
      //状态
      ActiveOptions: [{
        value: 1,
        label: '上架'
      }, {
        value: 0,
        label: '下架'
      }],


      // 搜索参数
      searchParams:[] ,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "商品编号不能为空", trigger: "blur" }
        ],
        stock: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        purchaseTime: [
          { required: true, message: "采购时间不能为空", trigger: "blur" }
        ],
        isActive: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "添加时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

    // 查询公共状态
    this.getDicts("has_status").then(response => {
      this.statusOptions = response.data;
    });

  },
  methods: {
    /** 查询商品列表 */
    getList() {
        this.searchParams = this.searchParam(this.queryParamsConfig, this.queryParams);
        this.loading = true;
      listItem(this.searchParams).then(response => {
        this.ItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        code: null,
        stock: null,
        purchaseTime: null,
        isActive: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 获取参数
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const name = row.id || this.ids
      getItem(name).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const names = row.id || this.ids;
      this.$confirm('是否确认删除商品编号为"' + names + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delItem(names);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 状态修改 **/
    handleStatusChange(row) {
      return changeStatusItem(row.id, row.status).then(response=>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      });
    },
    /** 修改字段状态 **/
    chanageFile(value , filed , id){
      return changeItemField(value , filed , id).then(response =>{
        if(response.code == 200){
          this.msgSuccess("操作成功");
        }
      }) ;
    } ,
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportItem(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
